package com.typy.www.tpay.ui.acitivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.typy.www.tpay.R;
import com.typy.www.tpay.base.BaseBackActivity;
import com.typy.www.tpay.util.listviewanimations.IntentUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 13799 on 2018/6/23.
 */

public class WebActivity extends BaseBackActivity {

    @BindView(R.id.webView)
    WebView webView;
    @BindView(R.id.pb_loading)
    ProgressBar pbLoading;
    private String url;

    public static void show(Context context, String title, String url) {
        Intent intent = new Intent();
        intent.putExtra("title", title);
        intent.putExtra("url", url);
        IntentUtil.intent2Activity(context, intent, WebActivity.class);
    }

    @Override
    protected void initView() {
        super.initView();
        ButterKnife.bind(this);
        setTitle(getIntent().getStringExtra("title"));
        url = getIntent().getStringExtra("url");
//        url="https://www.baidu.com/";
        initWeb();
    }

    private void initWeb() {

        WebSettings settings = webView.getSettings();
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setLoadsImagesAutomatically(true);
        settings.setJavaScriptEnabled(true);
        settings.setAppCacheEnabled(true);
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        settings.setDomStorageEnabled(true);

        webView.loadUrl(url);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    pbLoading.setVisibility(View.GONE);
                } else {
                    if (pbLoading.getVisibility() == View.GONE)
                        pbLoading.setVisibility(View.VISIBLE);
                    pbLoading.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }
        });
    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_web;
    }


}
